// packageD/auction/auctioneer-manage/set_password/set_password.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    yanzm: '',
    mima: '',
    imgcode: '',
    comfigmima: '',
    from: {
      country: "86",
      mobile: "",
      password: "",
      confirm_password: "",
      captcha: ""
    },
    codetext: '获取短信验证码',
    start1: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getimgdata();
  },
  getyzm() {
    console.log('获取验证码');
    if (this.data.start1) {
      return false;
    }
    let time = 60;
    let that = this;
    that.setData({
      "start1": true,
      "codetext": "(" + time + ")秒后重新获取"
    });
    let set = setInterval(function () {
      that.setData({
        codetext: "(" + --time + ")秒后重新获取",
      });
    }, 1000);
    setTimeout(function () {
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      clearInterval(set);
    }, 60000);
    that.verificationCode(set);
  },
  //发送验证码
  verificationCode(set) {
    let that = this;
    if (app._isTextEmpty(this.data.from.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '请填写手机号',
        duration: 1500
      });
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      clearInterval(set);
      return;
    }
    if (this.data.from.country == "86" && app._isMoblie(this.data.from.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '请填写正确的手机号',
        duration: 1500
      });
      return;
    }

    if (app._isTextEmpty(this.data.from.captcha) && this.data.imgcode) {
      wx.showToast({
        icon: 'none',
        title: '请填图形验证码',
        duration: 1500
      });
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      clearInterval(set);
      return;
    } else {
      let urlStr = app.getNetAddresss("member.register.sendWithdrawCode");
      urlStr += '&mobile=' + this.data.from.mobile;
      urlStr += '&state=' + this.data.from.country;
      urlStr += '&captcha=' + this.data.from.captcha;
      urlStr += '&code=' + this.data.from.code;
      urlStr += '&reset=1';
      urlStr += '&sms_type=2';
      // 发送获取验证码的请求
      app._getNetWork({
        url: urlStr,
        validateBol: 2,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              icon: 'none',
              title: '已发送',
              duration: 1500
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
            //刷新图形验证码
            that.setData({
              'form.captcha': '',
            });
            that.getimgdata();
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  value_one(e) {
    console.log(e.detail.value);
    this.setData({
      'from.mobile': e.detail.value
    });
    console.log(this.data.from);
  },
  value_two(e) {
    this.setData({
      'from.code': e.detail.value
    });
  },
  value_thr(e) {
    this.setData({
      'from.password': e.detail.value
    });
    console.log(this.data.from.password);
  },
  value_four(e) {
    this.setData({
      'from.confirm_password': e.detail.value
    });
  },
  confirm() {
    if (app._isTextEmpty(this.data.from.mobile)) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    if (app._isMoblie(this.data.from.mobile) && this.data.from.country == '86') {
      wx.showToast({
        title: '请填写正确的手机号',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    if (app._isTextEmpty(this.data.from.code)) {
      wx.showToast({
        title: '请填验证码',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    if (app._isTextEmpty(this.data.from.password)) {
      wx.showToast({
        title: '请填写密码',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    if (app._isTextEmpty(this.data.from.confirm_password)) {
      wx.showToast({
        title: '请填写确认密码',
        icon: 'none',
        duration: 1000
      });
      return;
    }

    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      art: 'setPassword',
      repeat: that.data.from.confirm_password,
      passwd: that.data.from.password,
      mobile: that.data.from.mobile,
      code: that.data.from.code
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
            success() {
              setTimeout(() => {
                wx.navigateTo({
                  url: '/packageD/auction/auctioneer-manage/auctioneer/auctioneer',
                });
              }, 1000);
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取图片验证码
  getimgdata() {
    let that = this;
    let urlStr = app.getNetAddresss("home-page.index");
    app._getNetWork({
      url: urlStr,
      validateBol: 2,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.captcha) {
            that.setData({
              imgcode: res.data.captcha.img
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tapin() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      art: 'setPassword',
      repeat: that.data.form.confirm_password,
      passwd: that.data.form.password,
      mobile: that.data.form.mobile,
      code: that.data.form.code
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});